package com.douqu.game.main.util;


//import com.douqu.game.core.entity.ext.TestInfo;

import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.factory.DatabaseConfig;
import com.douqu.game.core.factory.DatabaseFactory;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.SpringServer;
import com.douqu.game.main.database.mapper.read.ReadPlayerMapper;
import com.douqu.game.main.server.LoadManager;
import com.douqu.game.main.server.SpringContext;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
* Author : Bean
* 2017-07-06 15:29
*/

public class Test {



        public static void main(String[] args)
        {
//            testJDBC();

//            testMybatis();

        }




    public static void testMybatis()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper readPlayerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        for(int i = 0; i < 5; i++)
        {

            long t1 = System.currentTimeMillis();
            List<PlayerModel> list = readPlayerMapper.getAllPlayer();

            long t2 = System.currentTimeMillis();

            for(PlayerModel playerModel : list)
            {
                Player player = new Player(playerModel);
            }

            long t3 = System.currentTimeMillis();

            LogUtils.info("查询玩家数量:" + list.size() + " 总耗费:" + (t2-t1) + "毫秒");
            LogUtils.info("转换成Player:" + list.size() + " 总耗费:" + (t3-t2) + "毫秒");
        }


        System.exit(0);
    }

    public static void testJDBC()
    {
        DatabaseFactory databaseFactory = new DatabaseFactory(new DatabaseConfig("127.0.0.1", "3306", "slg111", "root", "123456"));

        for(int i = 0; i < 5; i++)
        {
            long t1 = System.currentTimeMillis();
            List<Map<String,Object>> dataList = databaseFactory.query("select * from player");
            long t2 = System.currentTimeMillis();
            LogUtils.info("查询玩家数量:" + dataList.size() + " 总耗费:" + (t2-t1) + "毫秒");
        }

        System.exit(0);

    }





    public static void test5(int time)
    {
        System.out.println("time -> " + time);
    }

    public static void test4()
    {
        BlockingQueue queue = new LinkedBlockingDeque<>(10);

    }

    public static void test3(String last, int count)
    {
        if(last.length() != 6)
            return;

        char[] charAry = last.toCharArray();
        char first = charAry[0];
        char second = charAry[1];
        int number = Integer.parseInt(charAry[2]+""+charAry[3]+""+charAry[4]+""+charAry[5]);

        LogUtils.debug("first  -> " + first);
        LogUtils.debug("second -> " + second);
        LogUtils.debug("number -> " + number);


        List<String> result = new ArrayList<>();

        int end = number + count;
        end = end > 9999 ? 9999 : end;
        StringBuilder buffer;
        for(int i = number + 1; i <= end; i++)
        {
            buffer = new StringBuilder();
            buffer.append(first);
            buffer.append(second);
            buffer.append(i);
            result.add(buffer.toString());
        }

        int needCount = count - result.size();

        LogUtils.debug("已经生成了 -> " + result.size() + "个CDK,还需要 -> " + needCount + "个");

        int temp = 1;
        int curCount = result.size();
        choose:for(int i = first; i <= 'Z'; i++)
        {
            for(int j = second+1; j <= 'Z'; j++)
            {
                for(int k = 1; k <= 9999; k++)
                {
                    buffer = new StringBuilder();
                    buffer.append((char)i);
                    buffer.append((char)j);
                    if(temp < 10)
                        buffer.append("000");
                    else if(temp < 100)
                        buffer.append("00");
                    else if(temp < 1000)
                        buffer.append("0");

                    buffer.append(temp++);
                    result.add(buffer.toString());

                    if(++curCount >= count)
                        break choose;
                }
            }
        }

        LogUtils.debug("所有CDK -> \n");
        LogUtils.debug(result);

        LogUtils.debug("最大的为 -> " + Collections.max(result));
    }

    public static void test2(int... targetIds)
    {


    }



    public static void testLoad() {
        InputStream input = null;
        try {
            String filePath = "data/InitData";
            File file = new File(CoreUtils.getBasePath() + filePath);
            if (!file.exists())
                file = new File(CoreUtils.getRootPath() + filePath);

            if (file.exists()) {
                LogUtils.info("用文件目录的方式加载");
                input = new BufferedInputStream(new FileInputStream(file));
                LogUtils.info("Utils.getBasePath():" + CoreUtils.getBasePath());
            } else {
                LogUtils.info("用文件流的方式加载");
                input = Test.class.getClassLoader().getResourceAsStream(filePath);
                LogUtils.info("Utils.getRootPath():" + CoreUtils.getRootPath());
            }

//            String result = FileUtils.readFile(input);

//            LogUtils.info("result:" + result);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    LogUtils.error(e.getMessage());
                }
            }
        }
    }


}
